package com.wangan.wanganpush.xml;

import com.wangan.wanganpush.model.Vehicle;
import com.wangan.wanganpush.props.MyProps;
import com.wangan.wanganpush.utils.AppConstants;
import com.wangan.wanganpush.utils.FileCreatUtils;
import com.wangan.wanganpush.utils.ImgDownloadUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class VehicleXml {

    @Resource
    MyProps myProps;

    public  String parseXml(List<Vehicle> urList,String messageCode,String fileName)

    {
        String filePath = myProps.getJarPath()+fileName;
        System.out.println(filePath);
        if (urList == null || urList.size() == 0) return null;

        try {
            StringBuffer sb = new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>  <MESSAGE>");
            sb.append("<DATASET name=\"WA_COMMON_010000\">  <DATA>");
            sb.append("<ITEM key=\"H010001\" val=\"" + AppConstants.applicationCoding + "\"/>");
            sb.append("<ITEM key=\"I010014\" val=\"" + messageCode + "\"/>");
            sb.append("<ITEM key=\"I010031\" val=\"" + "VEHICLEINFO" + "\"/>");
            sb.append("<ITEM key=\"I010013\" val=\"" + 3 + "\"/>");
            sb.append("</DATA> </DATASET> ");

            sb.append("<DATASET name=\"WA_COMMON_010022\">");

            for (int i = 0; i < urList.size(); i++) {
                Vehicle vehicle = urList.get(i);
                sb.append("<DATA>");
                if (vehicle.getVehicleId() != null) {
                    sb.append("<ITEM key=\"C030009\" val=\"" + vehicle.getVehicleId() + "\"/>");
                }

                if (vehicle.getVehiclePlate() != null) {
                    sb.append("<ITEM key=\"C030002\" val=\"" + vehicle.getVehiclePlate() + "\"/>");
                }

                if (vehicle.getVinNo() != null) {
                    sb.append("<ITEM key=\"C030003\" val=\"" + vehicle.getVinNo() + "\"/>");
                }

                if (vehicle.getVehicleEngineNo() != null) {
                    sb.append("<ITEM key=\"C030004\" val=\"" + vehicle.getVehicleEngineNo() + "\"/>");
                }

                if (vehicle.getVehicleBrand() != null) {
                    sb.append("<ITEM key=\"C010002\" val=\"" + vehicle.getVehicleBrand() + "\"/>");
                }

                if (vehicle.getVehicleModel() != null) {
                    sb.append("<ITEM key=\"C010003\" val=\"" + vehicle.getVehicleModel() + "\"/>");
                }

                if (vehicle.getVehicleColor() != null) {
                    sb.append("<ITEM key=\"C010004\" val=\"" + vehicle.getVehicleColor() + "\"/>");
                }

                if (vehicle.getVehicleType() != null) {
                    sb.append("<ITEM key=\"C030010\" val=\"" + vehicle.getVehicleType() + "\"/>");
                }

                if (vehicle.getVehicleSpy() != null) {
                    sb.append("<ITEM key=\"C030011\" val=\"" + vehicle.getVehicleSpy() + "\"/>");
                }

                if (vehicle.getVehicleBusiness() != null) {
                    sb.append("<ITEM key=\"C030012\" val=\"" + vehicle.getVehicleBusiness() + "\"/>");
                }

                if (vehicle.getVehicleInsurancePhoto() != null) {
                    String picureName = vehicle.getVehicleInsurancePhoto().split("/")[vehicle.getVehicleInsurancePhoto().split("/").length-1];
                    //创建临时文件夹
                    FileCreatUtils.creatFileDir(filePath);
                    //图片下载
                    ImgDownloadUtils.downloadPicture(vehicle.getVehicleInsurancePhoto(),filePath+"/"+picureName);

                    sb.append("<ITEM key=\"C030013\"  val=\"" +picureName  + "\"  ismedia=\"true\"/>");
                }

                if (vehicle.getVehicleLicense() != null) {
                    String picture = vehicle.getVehicleLicense().split("/")[vehicle.getVehicleLicense().split("/").length-1];
                    //创建临时文件夹
                    FileCreatUtils.creatFileDir(filePath);
                    //图片下载
                    ImgDownloadUtils.downloadPicture(vehicle.getVehicleLicense(),filePath+"/"+picture);
                    sb.append("<ITEM key=\"C030014\" val=\"" +picture  + "\" ismedia=\"true\"/>");
                }

                if (vehicle.getVehicleOfCompany() != null) {
                    sb.append("<ITEM key=\"E010002\" val=\"" + vehicle.getVehicleOfCompany() + "\"/>");
                }

                if (vehicle.getVehicleOfCity() != null) {
                    sb.append("<ITEM key=\"B030002\" val=\"" + vehicle.getVehicleOfCity() + "\"/>");
                }

                if (vehicle.getActionType() != null) {
                    sb.append("<ITEM key=\"H010003\" val=\"" + vehicle.getActionType() + "\"/>");
                }

                if (vehicle.getActionTime() != null) {
                    sb.append("<ITEM key=\"I010005\" val=\"" + vehicle.getActionTime() + "\"/>");
                }


                sb.append("</DATA>");
            }

            sb.append("</DATASET>");
            sb.append("</MESSAGE>");


            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
